package com.didi.gulimall.product.controller;

import com.didi.gulimall.common.utils.PageUtils;
import com.didi.gulimall.common.utils.R;
import com.didi.gulimall.product.entity.ProductAttrValueEntity;
import com.didi.gulimall.product.service.AttrService;
import com.didi.gulimall.product.service.ProductAttrValueService;
import com.didi.gulimall.product.vo.AttrResponseVO;
import com.didi.gulimall.product.vo.AttrVO;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author didi
 * @email 1062664904@qq.com
 * @date 2021-01-14 15:42:45
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 获取spu规格
     *  /product/attr/base/listforspu/{spuId}
     * @param spuId
     * @return
     */
    @GetMapping("base/listforspu/{spuId}")
    public R listforspu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> attrValues =  productAttrValueService.listforspu(spuId);

        return R.ok().put("data", attrValues);
    }

    /**
     * /product/attr/update/{spuId}
     * 修改
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuId(@PathVariable("spuId")Long spuId,
                         @RequestBody List<ProductAttrValueEntity> attrValueEntities) {
        productAttrValueService.updateSpuId(attrValueEntities,spuId);
        return R.ok();
    }
    /**
     * 列表
     */
    @GetMapping("{attrType}/list/{catelogId}")
    public R list(
            @ApiParam(name = "属性类型",value = "attrType",required = true)
            @PathVariable("attrType") String attrType,
            @ApiParam(name = "请求参数",value = "params",required = true)
            @RequestParam Map<String, Object> params,
            @ApiParam(name = "三级分类Id",value = "catelogId",required = true)
            @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrService.getAttrGroupList(params,catelogId,attrType);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrResponseVO attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody AttrVO attrVO) {
        attrService.saveAttr(attrVO);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody AttrVO attrVO) {
        attrService.updateAttr(attrVO);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete( @RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
